package com.zml.rpc.comsumer.config;

import com.zml.rpc.comsumer.proxy.RpcClientProxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Field;

public class RpcBeanPostProcessor implements BeanPostProcessor {
    @Autowired
    private RpcClientProxy rpcClientProxy;
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Field[] declaredFields = bean.getClass().getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            RpcService annotation = declaredFields[i].getAnnotation(RpcService.class);
            if(annotation!=null){
                rpcClientProxy.createProxy(declaredFields[i].getClass());
            }
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
